package cn.hznc.controller.service.production.logic;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.domain.entity.ProductionBomEntity;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author：Administrator
 * @Date：2023/5/29 8:25
 */
@Getter
@Setter
public class ProductionBomLogic{

    private final ProductionBomDo productionBomDo;

    private final List<ProductionBomEntity> originProductionBomList;

    private final List<ProductionBomEntity> updateProductionBomList;

    public ProductionBomLogic(List<ProductionBomEntity> originProductionBomList, List<ProductionBomEntity> updateProductionBomList) {
        this.originProductionBomList = originProductionBomList;
        this.updateProductionBomList = updateProductionBomList;
        this.productionBomDo=new ProductionBomDo();
    }

    /**
     * 获取处理后的业务模型
     *
     * @return subProgramDo
     */
    public ProductionBomDo getProductionBomDo() {
        return productionBomDo;
    }

    /**
     * 分离增删改数据
     */
    public void buildProcessBomDo(){
        if (CollectionUtil.isEmpty(originProductionBomList)){
            productionBomDo.addCreateElement(updateProductionBomList);
            return;
        }
        if (CollectionUtil.isEmpty(updateProductionBomList)){
            productionBomDo.addDeleteElement(originProductionBomList);
            return;
        }
        // 比对id判断，对删除，修改，新增
        List<Integer> ids = originProductionBomList.stream().map(ProductionBomEntity::getId).collect(Collectors.toList());
        List<Integer> updateIds = updateProductionBomList.stream().filter(updateProcessCraft-> ObjectUtil.isNotNull(updateProcessCraft.getId())).map(ProductionBomEntity::getId).collect(Collectors.toList());
        for (ProductionBomEntity productionBomEntity : updateProductionBomList) {
            // 添加修改元素
            if (ids.contains(productionBomEntity.getId())) {
                productionBomDo.addUpdateElement(productionBomEntity);
            }
            // 添加新增元素
            if (ObjectUtils.isEmpty(productionBomEntity.getId())){
                productionBomDo.addCreateElement(productionBomEntity);
            }
        }
        for (ProductionBomEntity productionBomEntity : originProductionBomList) {
            // 添加删除元素
            if (!updateIds.contains(productionBomEntity.getId())){
                productionBomDo.addDeleteElement(productionBomEntity);
            }
        }
    }
}
